<!--
  * 产品表单
  *
  * @Author:    1024创新实验室-主任：卓大
  * @Date:      2022-07-21 21:55:12
  * @Wechat:    zhuda1024
  * @Email:     lab1024@163.com
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
-->
<template>
  <a-modal :title="form.goodsId ? '编辑' : '添加'"
           :width="1000"
           :open="visible"
           :body-style="{ paddingBottom: '80px' }"
           @cancel="onClose">
    <a-form ref="formRef"
            :model="form"
            :rules="rules"
            :label-col="{ span: 5 }">
      <a-row>
        <a-col :span="12">
          <a-form-item label="产品分类"
                       name="categoryId">
            <CategoryTree v-model:value="form.categoryId"
                          placeholder="请选择产品分类"
                          :categoryType="CATEGORY_TYPE_ENUM.GOODS.value" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品名称"
                       name="goodsName">
            <a-input v-model:value="form.goodsName"
                     placeholder="请输入产品名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="型号"
                       name="model">
            <a-input v-model:value="form.model"
                     placeholder="型号" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="产品规格"
                       name="specification">
            <DictSelectByKey style="width: 100%"
                             key-code="good_unit"
                             v-model:value="form.specification" />
            <!-- <a-input v-model:value="form.specification"
                       placeholder="请输入产品规格" /> -->
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="参数"
                       name="parameter">
            <a-textarea style="width: 100%"
                        v-model:value="form.parameter"
                        placeholder="参数" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="供应商"
                       name="subcontractorInfoId">
            <a-select style="width: 100%"
                      v-model:value="form.subcontractorInfoId"
                      placeholder="供应商">
              <a-select-option v-for="item in registrantList"
                               :key="item"
                               :value="item.id">{{ item.companyName }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <!-- <a-form-item label="产品状态"
                       name="goodsStatus">
            <SmartEnumSelect enum-name="GOODS_STATUS_ENUM"
                             v-model:value="form.goodsStatus" />
          </a-form-item>
          <a-form-item label="产地"
                       name="place">
            <DictSelect key-code="GODOS_PLACE"
                        v-model:value="form.place" />
          </a-form-item>
          <a-form-item label="上架状态"
                       name="shelvesFlag">
            <a-radio-group v-model:value="form.shelvesFlag">
              <a-radio :value="true">上架</a-radio>
              <a-radio :value="false">下架</a-radio>
            </a-radio-group>
          </a-form-item> -->
        <a-col :span="12">
          <a-form-item label="产品价格"
                       name="price">
            <a-input-number style="width: 100%"
                            placeholder="请输入产品价格"
                            v-model:value="form.price"
                            :min="0" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="是否禁用"
                       name="disabledFlag">
            <a-select v-model:value="form.disabledFlag"
                      placeholder="请选择是否禁用"
                      style="width: 100%">
              <a-select-option value="1">是</a-select-option>
              <a-select-option value="0">否</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="备注"
                       name="remark">
            <a-textarea style="width: 100%"
                        placeholder="请输入备注"
                        v-model:value="form.remark" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <div :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1,
      }">
      <a-button style="margin-right: 8px"
                @click="onClose">取消</a-button>
      <a-button type="primary"
                @click="onSubmit">提交</a-button>
    </div>
  </a-modal>
</template>
<script setup>
import { ref, nextTick, reactive } from 'vue';
import CategoryTree from '/@/components/business/category-tree-select/index.vue';
import { CATEGORY_TYPE_ENUM } from '/@/constants/business/erp/category-const';
import { message } from 'ant-design-vue';
import { SmartLoading } from '/@/components/framework/smart-loading';
import { GOODS_STATUS_ENUM } from '/@/constants/business/erp/goods-const';
import _ from 'lodash';
import { goodsApi } from '/@/api/business/goods/goods-api';
import { smartSentry } from '/@/lib/smart-sentry';
import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
import DictSelect from '/@/components/support/dict-select/index.vue';
import DictSelectByKey from '/@/components/support/dict-select-by-key/index.vue';
import { concludeSignApi } from '/@/api/business/conclude_sign/conclude_sign-api';

// emit
const emit = defineEmits(['reloadList']);

// 组件ref
const formRef = ref();

const formDefault = {
  //商品分类
  categoryId: undefined,
  //商品名称
  goodsName: undefined,
  //商品状态
  goodsStatus: GOODS_STATUS_ENUM.APPOINTMENT.value,
  //产地
  place: undefined,
  //商品价格
  price: undefined,
  //上架状态
  shelvesFlag: true,
  //备注
  remark: '',
  //商品id
  goodsId: undefined,
  //商品规格
  specification: undefined,
  //供应商
  subcontractorInfoId: undefined,
  //型号
  model: undefined,
  //参数
  parameter: undefined,
  //是否禁用
  disabledFlag: undefined,
};
let form = reactive({ ...formDefault });
const rules = {
  categoryId: [{ required: true, message: '请选择产品分类' }],
  goodsName: [{ required: true, message: '产品名称不能为空' }],
  // goodsStatus: [{ required: true, message: '商品状态不能为空' }],
  price: [{ required: true, message: '产品价格不能为空' }],
  // place: [{ required: true, message: '产地不能为空' }],
  model: [{ required: true, message: '请填写型号' }],
  disabledFlag: [{ required: true, message: '请选择' }],
};
// 是否展示抽屉
const visible = ref(false);

function showDrawer (rowData) {
  Object.assign(form, formDefault);
  if (rowData && !_.isEmpty(rowData)) {
    Object.assign(form, rowData);
  }
  if (form.place && form.place.length > 0) {
    form.place = form.place[0].valueCode;
  }
  console.log(form);
  visible.value = true;
  nextTick(() => {
    formRef.value.clearValidate();
  });
}

function onClose () {
  Object.assign(form, formDefault);
  visible.value = false;
}

// 供应商
const registrantList = ref([]);
async function suppliction () {
  let res = await concludeSignApi.SupplierList();
  registrantList.value = res.data;
}

suppliction();

// 提交
function onSubmit () {
  formRef.value
    .validate()
    .then(async () => {
      SmartLoading.show();
      try {
        let params = _.cloneDeep(form);
        if (params.place && Array.isArray(params.place) && params.place.length > 0) {
          params.place = params.place[0].valueCode;
        }
        if (params.specification && Array.isArray(params.specification) && params.specification.length > 0) {
          params.specification = params.specification[0];
        }
        if (form.goodsId) {
          await goodsApi.updateGoods(params);
        } else {
          await goodsApi.addGoods(params);
        }
        message.success(`${form.goodsId ? '修改' : '添加'}成功`);
        onClose();
        emit('reloadList');
      } catch (error) {
        smartSentry.captureError(error);
      } finally {
        SmartLoading.hide();
      }
    })
    .catch((error) => {
      console.log('error', error);
      message.error('参数验证错误，请仔细填写表单数据!');
    });
}

defineExpose({
  showDrawer,
});
</script>
